package com.meishij.fragment.finddetail;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.meishij.R;
import com.meishij.adapter.FindZhuanTiRecyclerAdapter;
import com.meishij.entry.FindCaiDanEntry;
import com.meishij.entry.FindZhuanTiEntry;
import com.meishij.server.FindServer;
import com.meishij.xml.FindDetailXmlUtils;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by 阿飞 on 2016/10/14.
 */

public class FindZhuanTiActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = FindZhuanTiActivity.class.getSimpleName();
    private Retrofit retrofit;

    private View currentView;
    private View huoDong;
    private View shiQu;
    private FindZhuanTiRecyclerAdapter huodongAdapter;
    private FindZhuanTiRecyclerAdapter shiQuAdapter;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_detail_zhuanti);
        initRetrofit();
        initView();
    }

    private void initView() {
        View back = findViewById(R.id.iv_find_detail_header_back);
        back.setOnClickListener(this);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_find_detail_zhuanTi);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        huodongAdapter = new FindZhuanTiRecyclerAdapter(this, null, FindZhuanTiRecyclerAdapter.TYPE_HUODONG);
        shiQuAdapter = new FindZhuanTiRecyclerAdapter(this, null, FindZhuanTiRecyclerAdapter.TYPE_SHIQU);

        huoDong = findViewById(R.id.tv_find_detail_zhuanTi_huoDong);
        huoDong.setOnClickListener(this);
        shiQu = findViewById(R.id.tv_find_detail_zhuanTi_shiQu);
        shiQu.setOnClickListener(this);
        currentView = new View(this);

        changeCurrentIndex(huoDong);
    }

    private void initRetrofit() {
        retrofit = new Retrofit.Builder()
                .baseUrl(FindServer.BaseUrl)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    }

    private void requestNetData(String t) {
        FindServer findServer = retrofit.create(FindServer.class);

        Call<String> call = findServer.getCaiDanResult(t);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                String xml = response.body();
                switch (currentView.getId()) {
                    case R.id.tv_find_detail_zhuanTi_huoDong:
                        FindZhuanTiEntry huodongEntry = FindDetailXmlUtils.xml2FindZhuanTiEntry(xml);
                        huodongAdapter.clear();
                        huodongAdapter.addAll(huodongEntry.getData());
                        recyclerView.setAdapter(huodongAdapter);
                        break;
                    case R.id.tv_find_detail_zhuanTi_shiQu:
                        FindCaiDanEntry shiQuEntry = FindDetailXmlUtils.xml2FindCaiDanEntry(xml);
                        shiQuAdapter.clear();
                        shiQuAdapter.addAll(shiQuEntry.getData());
                        recyclerView.setAdapter(shiQuAdapter);
                        break;
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                t.printStackTrace();
                Toast.makeText(FindZhuanTiActivity.this, "网络请求失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void changeCurrentIndex(View toView) {
        if (toView != currentView) {
            currentView.setSelected(false);
            currentView = toView;
            toView.setSelected(true);
            switch (toView.getId()) {
                case R.id.tv_find_detail_zhuanTi_huoDong:
                    requestNetData("3");
                    recyclerView.setAdapter(null);
                    break;
                case R.id.tv_find_detail_zhuanTi_shiQu:
                    requestNetData("4");
                    recyclerView.setAdapter(null);
                    break;
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_find_detail_header_back:
                finish();
                break;
            case R.id.tv_find_detail_zhuanTi_huoDong:
                changeCurrentIndex(huoDong);
                break;
            case R.id.tv_find_detail_zhuanTi_shiQu:
                changeCurrentIndex(shiQu);
                break;
        }
    }
}
